//
//  DHArticleCommentCell.m
//  Dresshere
//
//  Created by ZhangPeng on 2018/1/8.
//  Copyright © 2018年 张朋. All rights reserved.
//

#import "DHArticleCommentCell.h"
#import "FashionArticle.h"
#import "FashionArticle.h"

@interface DHArticleCommentCell()

@property (weak, nonatomic) IBOutlet UIImageView *headImgView;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *disLabel;
@property (weak, nonatomic) IBOutlet UILabel *timeLabel;
@property (weak, nonatomic) IBOutlet UIButton *likeButton;
@property (weak, nonatomic) IBOutlet UILabel *likeLabel;
@property (weak, nonatomic) IBOutlet UILabel *contentLabel;

@property (strong, nonatomic) FashionArticleComment *comment;

@property (strong, nonatomic) NSDictionary *dic; //新版本数据

@end

@implementation DHArticleCommentCell

- (void)awakeFromNib {
    [super awakeFromNib];
    
    self.headImgView.layer.cornerRadius = self.headImgView.height / 2;
    
    WeakSelf(weakSelf)
    [self.likeButton bk_addEventHandler:^(id sender) {
        if (![UIImagePNGRepresentation(weakSelf.likeButton.imageView.image) isEqual:UIImagePNGRepresentation([UIImage imageNamed:@"magazine_like"])]){
            if (weakSelf.isCommunityComment) {
                [weakSelf.likeButton setImage:[UIImage imageNamed:@"magazine_like"] forState:UIControlStateNormal];
                weakSelf.likeLabel.text = [NSString stringWithFormat:@"%ld", [weakSelf.comment.nice_count integerValue] + 1];
                [[DHNetAPIClient shareDHNetAPIClient] request_social_post_comments_niceWithParams:@{@"comment_id":[weakSelf.dic objectForKey:@"_id"]} andBlock:^(id data, NSError *error) {
                    
                }];
            }else{
                [weakSelf.likeButton setImage:[UIImage imageNamed:@"magazine_like"] forState:UIControlStateNormal];
                weakSelf.likeLabel.text = [NSString stringWithFormat:@"%ld", [weakSelf.comment.nice_count integerValue] + 1];
                [[DHNetAPIClient shareDHNetAPIClient] request_articles_comment_niceWithCommentID:[weakSelf.dic objectForKey:@"_id"] andBlock:^(id data, NSError *error) {
                    
                }];
            }
        }
    } forControlEvents:UIControlEventTouchUpInside];
}

- (void)updateWith:(FashionArticleComment *)comment{
    self.comment = comment;
    [self.headImgView dh_setImageURLString:comment.owner.photo];
    self.nameLabel.text = comment.owner.nickname.length > 0? comment.owner.nickname : [NSString hindPhoneString4Char:comment.owner.phone];
    NSDate *date = [NSDate dateWithServerISOString:comment.createdAt format:@"yyyy-MM-dd'T'HH:mm:ss.SSSZ"];
    self.timeLabel.text = [NSDate stringFromDate:date withFormat:@"yyyy/MM/dd HH:mm"];
    self.contentLabel.text = comment.content;
    self.likeLabel.text = [NSString stringWithFormat:@"赞(%@)", comment.nice_count];
}

- (void)updateWithDic:(NSDictionary *)comment{
    self.dic = comment;
    [self.headImgView dh_setImageURLString:[[self.dic objectForKey:@"owner"] objectForKey:@"photo"]];
    self.nameLabel.text = [[self.dic objectForKey:@"owner"] objectForKey:@"nickname"];
    self.disLabel.text = [[self.dic objectForKey:@"owner"] objectForKey:@"title"];
    NSDate *date = [NSDate dateWithServerISOString:[self.dic objectForKey:@"createdAt"] format:@"yyyy-MM-dd'T'HH:mm:ss.SSSZ"];
    self.timeLabel.text = [NSDate stringFromDate:date withFormat:@"yyyy/MM/dd HH:mm"];
    self.contentLabel.text = [self.dic objectForKey:@"content"];
    self.likeLabel.text = [NSString stringWithFormat:@"%@", [self.dic objectForKey:@"nice_count"]];
    if ([[comment objectForKey:@"niced_by_self"] boolValue]) {
        [self.likeButton setImage:[UIImage imageNamed:@"magazine_like"] forState:UIControlStateNormal];
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
